package at.ac.uniklu.rallyemaster;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import at.ac.uniklu.rallyemaster.activities.InformationActivity;
import at.ac.uniklu.rallyemaster.domain.TrackingPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ShowTrackMapOverlay extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays; 
	private ArrayList<TrackingPoint> trackingPoints;
	private Activity activity;
	private Drawable marker;
	private String name;

	
	public ShowTrackMapOverlay(Drawable defaultMarker,Activity activity, String name,ArrayList<TrackingPoint> trackingPoints) {
		super(defaultMarker);
		marker = defaultMarker;
		this.trackingPoints = trackingPoints;
		this.activity = activity;
		mOverlays = new ArrayList<OverlayItem>();
		this.name = name;
		populate();
	}

	@Override
	protected boolean onTap(int i) {
		// TODO Auto-generated method stub
		RallyeMaster.logDebug("Taped on " + name +" with GeoPoint: " + i + "with ID: " + trackingPoints.get(i).getId());
		Intent intent = new Intent(activity, InformationActivity.class);
		intent.putExtra("PointID", trackingPoints.get(i).getId());
		activity.startActivity(intent);
		return super.onTap(i);
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlayItem(OverlayItem item) {
	     marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		 boundCenter(marker);
		 item.setMarker(marker);
		 mOverlays.add(item);
		 RallyeMaster.logDebug( name +" OverlayItem "+ mOverlays.indexOf(item)+" added with ID" + trackingPoints.get(mOverlays.indexOf(item)).getId());
	     RallyeMaster.logDebug("Size of "+name + " OverlayItems: " + size());
	      populate();

	 }

	public void clearOverlay() {
		RallyeMaster.logDebug("Clear Overlay " + name);
		mOverlays.clear();
		
	}

	

}
